package com.moyuaninfo.myyserver.controller;

import com.moyuaninfo.myyhikwork.hik.HikSdk;
import com.moyuaninfo.myyserver.entity.Dvr;
import com.moyuaninfo.myyserver.enums.GetLastErrorEnum;
import com.moyuaninfo.myyserver.framework.base.BaseController;
import com.moyuaninfo.myyserver.framework.json.JsonResult;
import com.moyuaninfo.myyserver.utils.QueueUtils;
import com.moyuaninfo.myyserver.vo.HikPtzControlOtherVo;
import com.moyuaninfo.myyserver.vo.HikPtzPresetOtherVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 海康威视云台控制
 *
 * @author QiuXiangBa
 * @version 1.0
 **/
@Slf4j
@Api(value = "HikVideoController", tags = "云台控制")
@RestController("HikOperateController")
public class HikOperateController extends BaseController {

    @Autowired
    private QueueUtils queueUtils;

    @ApiOperation(value = "云台控制操作", notes = "云台基本控制")
    @RequestMapping(value = "/hik/ptzControlOther", method = {RequestMethod.POST})
    public JsonResult hikPtzControlOther(@RequestBody @Valid HikPtzControlOtherVo hikPtzControlOtherVo) {
        int flag = -1;
        HikSdk hik = new HikSdk(hikPtzControlOtherVo.getDvr().getDvrIp(), (short) hikPtzControlOtherVo.getDvr().getDvrPort(), hikPtzControlOtherVo.getDvr().getDvrUsername(), hikPtzControlOtherVo.getDvr().getDvrPassword());
        try {
            // 初始化SDK
            hik.initSdk();

            // 注册设备
            flag = hik.loginSdk();
            if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
                return super.retFail(flag, "注册设备失败");
            }

            // 操作设备
            flag = hik.hikPtzControlWithSpeed(hikPtzControlOtherVo.getDvr().getDvrChannel(), hikPtzControlOtherVo.getDwPtzCommand().getValue(), hikPtzControlOtherVo.getDwStop(), hikPtzControlOtherVo.getDwSpeed());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 注销设备
            hik.logoutSdk();

            // 释放SDK资源
            hik.cleanUp();
        }

        // 返回操作结果
        if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
            return super.retFail(flag, "操作设备失败");
        }

        // 0.5s 后结束任务
        hikPtzControlOtherVo.setDwStop(1);
        queueUtils.put(hikPtzControlOtherVo, 1000);

        return super.retSuccess(null, "操作成功");
    }

    @ApiOperation(value = "重启设备")
    @RequestMapping(value = "/hik/reboot", method = {RequestMethod.POST})
    public JsonResult hikReboot(@RequestBody Dvr dvr) {
        HikSdk hik = new HikSdk(dvr.getDvrIp(), (short) dvr.getDvrPort(), dvr.getDvrUsername(), dvr.getDvrPassword());

        int flag = -1;
        try {
            // 初始化SDK
            hik.initSdk();

            // 注册设备
            flag = hik.loginSdk();
            if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
                return super.retFail(flag, "注册设备失败");
            }

            // 重启设备
            flag = hik.hikReboot();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 注销设备
            hik.logoutSdk();
            // 释放SDK资源
            hik.cleanUp();
        }

        // 返回操作结果
        if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
            return super.retFail(flag, "操作设备失败");
        }
        return super.retSuccess(null, "操作成功");
    }

    @ApiOperation(value = "关闭设备")
    @RequestMapping(value = "/hik/shutDown", method = {RequestMethod.POST})
    public JsonResult hikShutDown(@RequestBody Dvr dvr) {
        HikSdk hik = new HikSdk(dvr.getDvrIp(), (short) dvr.getDvrPort(), dvr.getDvrUsername(), dvr.getDvrPassword());

        int flag = -1;
        try {
            // 初始化SDK
            hik.initSdk();

            // 注册设备
            flag = hik.loginSdk();
            if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
                return super.retFail(flag, "注册设备失败");
            }

            // 关闭设备
            flag = hik.hikShutDown();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 注销设备
            hik.logoutSdk();
            // 释放SDK资源
            hik.cleanUp();
        }

        // 返回操作结果
        if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
            return super.retFail(flag, "操作设备失败");
        }
        return super.retSuccess(null, "操作成功");
    }

    @ApiOperation(value = "云台预置点操作")
    @RequestMapping(value = "/hik/ptzPresetOther", method = {RequestMethod.POST})
    public JsonResult ptzPresetOther(@RequestBody HikPtzPresetOtherVo vo) {
        HikSdk hik = new HikSdk(vo.getDvr().getDvrIp(), (short) vo.getDvr().getDvrPort(), vo.getDvr().getDvrUsername(), vo.getDvr().getDvrPassword());

        int flag = -1;
        try {
            // 初始化SDK
            hik.initSdk();

            // 注册设备
            flag = hik.loginSdk();
            if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
                return super.retFail(flag, "注册设备失败");
            }

            // 云台预置点操作
            flag = hik.ptzPresetOther(vo.getDvr().getDvrChannel(), vo.getDwPtzPresetCmd().getValue(), vo.getDwPresetIndex());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 注销设备
            hik.logoutSdk();
            // 释放SDK资源
            hik.cleanUp();
        }

        // 返回操作结果
        if (GetLastErrorEnum.NET_DVR_NOERROR.getValue() != flag) {
            return super.retFail(flag, "操作设备失败");
        }
        return super.retSuccess(null, "操作成功");
    }
}
